package com.lin.lazybone.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lin.lazybone.R;
import com.lin.lazybone.application.BaseActivity;
import com.lin.lazybone.entity.Order;
import com.lin.lazybone.entity._User;
import com.lin.lazybone.utils.L;
import com.lin.lazybone.utils.UtilTools;

import java.util.Date;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by mr.lin on 2017/10/30.
 */

public class OrderActivity extends BaseActivity implements View.OnClickListener
{
    private ImageView orderlist_express_Image;
    private CircleImageView orderdetail_touxiang;
    private TextView orderdetail_sender;
    private TextView orderdetail_sendtime;
    private TextView orderdetail_price;
    private TextView orderdetail_content;
    private Button orderdetail_button_qiang;
    private TextView orderdetail_from;
    private TextView orderdetail_to;
    private Button orderdetail_chatbt;
    private LinearLayout orderdetail_uidlinear;
    private TextView orderdetail_uid;
    private Order order = null;
    private int type = 0;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        Toolbar toolbar = (Toolbar) findViewById(R.id.orderdetail_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
        {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        initData();
        initView();

    }

    private void initData()
    {
        Intent intent = getIntent();
        order = (Order) intent.getSerializableExtra("order_data");
        type = intent.getIntExtra("type", 0);
    }

    private void initView()
    {
        orderlist_express_Image = (ImageView) findViewById(R.id.orderlist_express_Image);
        setExpressImage();
        orderdetail_touxiang = (CircleImageView) findViewById(R.id.orderdetail_touxiang);
        orderdetail_sender = (TextView) findViewById(R.id.orderdetail_sender);
        orderdetail_sender.setText(order.getOrder_aid());
        orderdetail_sendtime = (TextView) findViewById(R.id.orderdetail_sendtime);
        orderdetail_sendtime.setText(UtilTools.getTime(order.getCreatedAt()));
        orderdetail_price = (TextView) findViewById(R.id.orderdetail_price);
        orderdetail_price.setText(order.getOrder_price());
        orderdetail_content = (TextView) findViewById(R.id.orderdetail_content);
        orderdetail_content.setText(order.getOrder_content());
        orderdetail_from = (TextView) findViewById(R.id.orderdetail_from);
        orderdetail_from.setText(order.getOrder_from());
        orderdetail_to = (TextView) findViewById(R.id.orderdetail_to);
        orderdetail_to.setText(order.getOrder_to());
        orderdetail_button_qiang = (Button) findViewById(R.id.orderdetail_button_qiang);
        orderdetail_button_qiang.setOnClickListener(this);
        orderdetail_uidlinear = (LinearLayout) findViewById(R.id.orderdetail_uidlinear);
        orderdetail_chatbt = (Button) findViewById(R.id.orderdetail_chatbt);
        orderdetail_chatbt.setOnClickListener(this);
        orderdetail_uid = (TextView) findViewById(R.id.orderdetail_uid);
        //如果已经被抢单，显示抢单者的信息
        if (order.getOrder_state() > 1)//待送达和已送达都可联系抢单者
        {
            orderdetail_uid.setText(order.getOrder_uid());
        }else
        {
            orderdetail_uidlinear.setVisibility(View.GONE);
        }

        //判断是从赚钱来，还是用户自己列表来
        orderdetail_button_qiang.setVisibility(type == 1 ? View.INVISIBLE : View.VISIBLE);
    }

    private void setExpressImage()
    {
        switch (order.getOrder_del_type())
        {
            case "顺丰":
                orderlist_express_Image.setBackgroundResource(R.mipmap.sf);
                break;
            case "申通":
                orderlist_express_Image.setBackgroundResource(R.mipmap.sto);
                break;
            case "中通":
                orderlist_express_Image.setBackgroundResource(R.mipmap.zto);
                break;
            case "EMS":
                orderlist_express_Image.setBackgroundResource(R.mipmap.ems);
                break;
            case "百世汇通":
                orderlist_express_Image.setBackgroundResource(R.mipmap.best);
                break;
            case "圆通":
                orderlist_express_Image.setBackgroundResource(R.mipmap.yto);
                break;
            case "韵达":
                orderlist_express_Image.setBackgroundResource(R.mipmap.yunda);
                break;
        }
    }


    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.orderdetail_button_qiang:
//                Toast.makeText(this, "点击了抢单按钮", Toast.LENGTH_SHORT).show();
                Order orderUpdata = new Order();
                orderUpdata.setOrder_state(2);
                orderUpdata.setOrder_uid(BmobUser.getCurrentUser(_User.class).getUsername());
                orderUpdata.update(order.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            Toast.makeText(OrderActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(OrderActivity.this, "更新失败", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }
                });
                finish();
                break;
            case R.id.orderdetail_chatbt:
                Intent intent = new Intent(this, ChatActivity.class);
                intent.putExtra("order_data", order);
                intent.putExtra("FromWhere", 0);
                startActivity(intent);
                break;

        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
